<?php
class CategoryService
{
	private $searchService;
	private $categoryDao;
	
	public function __construct()
	{
		$this->categoryDao		= new CategoryDao();
		$this->searchService	= C('SearchService');
	}
	
	public function get($id)
	{
		return $this->categoryDao->get($id);
	}
	
	public function add($data)
	{
		$result	= $this->categoryDao->add($data);
		$this->makeJsCategory();
		return $result;
	}
	
	public function del($id)
	{
		$list		= $this->getCategoryListByPId($id,$sub);
		if($list)
		{
			foreach($list as $lv)
			{
				$this->del($lv['id']);
			}
		}
		$this->categoryDao->del($id);
		$this->makeJsCategory();
	}
	
	public function hideCategory($id,$hide)
	{
		$this->update($id,array('is_hide'=>$hide));
		$subList	= $this->getCategoryListByPId($id);
		if($subList)
		{
			foreach($subList as $sv)
			{
				$this->hideCategory($sv['id'],$hide);
			}
		}
	}
	
	public function update($id,$data)
	{
		$result	= $this->categoryDao->update($id,$data);
		$this->makeJsCategory();
		return $result;
	}
	
	public function getParentList($id,$self=true)
	{
		$list	= array();
		$info	= $this->categoryDao->get($id);

		if($info)
		{
			if($info['pid']>0)
			{
				$list	= $this->getParentList($info['pid'],true);
			}
			if($self)
			{
				if(!$list)
				{
					$list	= array();
				}
				$list[]	= $info;
			}
		}
		return $list;
	}
	
	public function checkIdAndPid($id,$pid)
	{
		$list	= $this->getParentList($pid);
		if($list)
		{
			foreach($list as $lv)
			{
				if($lv['id']==$id)
				{
					return false;
				}
			}
		}
		return true;
	}
	
	public function getCategoryListByPId($pid, $update=false)
	{
		$condition['where']['expression'] 	= 'pid = '.$pid;
		$condition['orderby'] 				= 'orderby DESC,id ASC';
		return $this->categoryDao->fetchRows($condition);
	}
	
	public function getTopShowCategoryList()
	{
		$condition['where']['expression'] 	= 'is_hide=0 and pid = 0';
		$condition['orderby'] 				= 'orderby DESC,id ASC';
		$list		= $this->categoryDao->fetchRows($condition);
		return $list;
	}
	
	public function getSitemapCategoryList($start, $num)
	{
		$condition['where']['expression'] 	= 'is_hide=0';
		$condition['orderby'] 				= 'pid ASC, orderby DESC,id ASC';
		$result['count'] 					= $this->categoryDao->count($condition);
		if($result['count']>0)
		{
			$condition['limit'] 			= $num;
			$condition['offset'] 			= $start;
			$result['rows']					= $this->categoryDao->fetchRows($condition);
		}
		return $result;
	}
	
	public function makeJsCategory()
	{
		$jsFile		= APP_WEB_PATH.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'category.js';
		$condition['where']['expression'] 	= 'is_hide = 0';
		$condition['orderby'] 				= 'orderby DESC,id ASC';
		$list		= $this->categoryDao->fetchRows($condition);
		$all		= array();
		$multi		= array();
		foreach($list as $cv)
		{
			$tmp							= array('name'=>$cv['name'],'value'=>$cv['id'],'pid'=>$cv['pid']);
			$all[$cv['id']]					= $tmp;
			$multi['multi_'.$cv['pid']][]	= $tmp;
		}
		
		$content	= 'var dataAllCategory='.json_encode($all).';';
		$content	.= 'var dataMultiCategory='.json_encode($multi).';';
		return file_put_contents($jsFile, $content);
	}
}
?>